var cookie = new Cookie();
var addIp = "1.14.68.137:8000";
// 注册按钮
$('.zhuce_er').click(function(){
      display('none','block','none')
    $('.verification').attr('src',"http://"+addIp+"/image_code/?uuid="+uuid());
})

// 返回登录按钮
$('.return_to_login').click(function(){
    $('.verification').attr('src',"http://"+addIp+"/image_code/?uuid="+uuid());
    display('block','none','none')
})

// 忘记密码按钮
$('.forget_password').click(function(){
    display('none','none','block')
})


var uid = uuid();
$('.verification').attr('src',"http://"+addIp+"/image_code/?uuid="+uid);
$('.verification').click(function(){
	uid = uuid();
    $('.verification').attr('src',"http://"+addIp+"/image_code/?uuid="+uid);
})

$('.username_regexp').focus(function(){
    move('.user_format>p');
})

$('.password_regexp').focus(function(){
    move('.pass_format>p');
})

$('.password_regexp').blur(function(){
    regexp('','.password_regexp');
})

$('.confirm_password').focus(function(){
    move('.conf_format>p');
    $('.confirm_password').val("");
})

$('.mailbox_regexp').focus(function(){
    move('.mail_format>p');
})

$('.confirm_password').blur(function(){
    if($('.password_regexp').val() !== $('.confirm_password').val()){
        $('.conf_format').append($('<p style="color:red;font-size:14px">密码格式错误</p>'));
    }
})




/* 登录 */
    var sginReg = /^[A-z][A-z0-9|.]{5,11}/;
    var pwdReg = /^[A-z0-9][A-z0-9|_|-|.|+|*|/|-|@|#|$|%|&]{5,}/;
	$(".log_user").focus(function () {
		$(this).parent().children("p").remove();
	});
	$(".log_pass").blur(function () {
		if ($(this).val()) {
			if (!pwdReg.test($(this).val())) {
				$(this).parent().append($('<p style="color:red;font-size:14px">密码格式错误</p>'));
				$(this).val("");
			}
		}
	})
	$(".log_pass").focus(function () {
		$(this).parent().children("p").remove();
	})
	$(".btn").click(function () {
		if ($(".log_user").val() && $(".log_pass").val() && $(".log_ver").val()) {
			$.post("http://"+addIp+"/login/", {
				username: $(".log_user").val(),
				password: $(".log_pass").val(),
				uuid: uid,
				code: $(".log_ver").val()
			}, function (x) {
				console.log(x)
				cookie.set("token", x.token, 60*60*24*3)
				cookie.set("user", $(".log_user").val(), 60*60*24*3)
				alert(x.msg)
				if (x.code == 0) {
					location.href = "./index.html"
				}
			})
		}
	})
	
	
	


/* 注册 */
	$(".username_regexp").blur(function () {
		if($(this).val()) {
			regexp(this, "", "");
			$.post("http://"+addIp+"/verify_username/", {
				username: $(this).val()
			}, function (x) {
				if (x.code === -1) {
					$(".username_regexp").parent().append($('<p style="color:red;font-size:14px">'+ x.errmsg +'</p>'));
					$(".username_regexp").val("");
				}
			})
		}
	})



	$('.mailbox_regexp').blur(function(){
		regexp("","",'.mailbox_regexp');
		$.post("http://"+addIp+"/email/", {
			email: $(this).val()
		}, function (x) {
			console.log(x)
			cookie.set("email", x.no, 60*60*24*3)
		})
	})


	$(".btn_er").click(function () {
		$.post("http://"+addIp+"/register/", {
			username: $(".username_regexp").val(),
			password: $(".confirm_password").val(),
			no: cookie.get("email"),
			text: $(".code").val()
		}, function (x) {
			alert(x.msg)
		})
	})


/* 找回密码 */
	$('.send_code').click(function(){
		clearInterval(itme)
		var second = 60;
		var itme=setInterval(function(){
			$('.send_code').html(second+'s')
			second--;
			if(second == 0) {
			   clearInterval(itme) 
			   $('.send_code').html('发送验证码')
			}
		},1000)
	})





function regexp(element,password,postbox) {
    var regexp2 = /^[A-z][A-z0-9|.]{5,11}/;
    var regexp3 = /^[A-z0-9][A-z0-9|_|-|.|+|*|/|-|@|#|$|%|&]{5,}/;
    var regexp4 = /[A-z0-9_-]+(\.[A-z0-9_-]+)+/;
    if($(element).val()) {
        if(!regexp2.test($(element).val())) {
			$(element).parent().append($('<p style="color:red;font-size:14px">用户名格式错误</p>'));
        }
    }
    if($(password).val()) {
        if(!regexp3.test($(password).val())) {
            $('.pass_format').append($('<p style="color:red;font-size:14px">密码格式错误</p>'));
        }
    }
    if($(postbox).val()) {
        if(!regexp4.test($(postbox).val( ))) {
            $('.mail_format').append($('<p style="color:red;font-size:14px">邮箱格式错误</p>'));
        }
    }
}

function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}

function display(sign_state,enroll_state,retrieve_state) {
    $('.non').css({
        display:sign_state
    });
    $('.non_er').css({
        display:enroll_state
    });
    $('.non_three').css({
        display:retrieve_state
    });
}

function move(remove) {
    //删除正则验证没通过后的p元素
    $(remove).remove();
}